package com.oocl.sc.action;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.oocl.sc.entity.Suser;
import com.oocl.sc.service.SuserService;
import com.oocl.sc.service.impl.SuserServiceImpl;

public class LoginAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static final String SUCCESS = "{\"status\":\"1\"}";
	private static final String FAIL = "{\"status\":\"-1\"}";
	SuserService suserService = new SuserServiceImpl();

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String account = request.getParameter("account");
			String password = request.getParameter("password");
			if (account!=null && password!=null) {
				Suser suser = suserService.login(account, password);
				if (suser != null) {
					HttpSession session = request.getSession();
					session.setAttribute("usid", suser.getUsid());
					session.setAttribute("account", suser.getAccount());
					response.getWriter().write(SUCCESS);					
				}else{
					response.getWriter().write(FAIL);
				}				
			}else{				
				response.getWriter().write(FAIL);
			}
		} catch (Exception e) {
			response.getWriter().write(FAIL);
			e.printStackTrace();
		}
	}

}
